package com.chen.spring.cloud.service.admin.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chen.common.BaseController;
import com.chen.entity.ResourceCategory;
import com.chen.entity.SignIn;
import com.chen.spring.cloud.service.admin.service.IIndexLinkService;
import com.chen.spring.cloud.service.admin.service.IResourceCategoryService;
import com.chen.spring.cloud.service.admin.service.IResourceService;
import com.chen.spring.cloud.service.admin.service.ISignInService;
import com.chen.vo.ResCateVo;
import com.chen.vo.ResListVo;
import com.chen.vo.ReservationVo;
import com.chen.vo.YuYueVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 系统设置 前端控制器
 * </p>
 *
 * @author 陈永鹏
 * @since 2019-03-25
 */
@RestController
@RequestMapping("/Index")
public class IndexController extends BaseController {

    @Autowired
    private IIndexLinkService indexLinkService;

    @Autowired
    private IResourceService resourceService;

    @Autowired
    private ISignInService signInService;

    @Autowired
    private IResourceCategoryService resourceCategoryService;

    @RequestMapping("/index")
    public Map<String,Object> index(@RequestParam(value = "u_id") Integer u_id){
        Map<String,Object> result = new HashMap<>();
        YuYueVo yuYueVo = indexLinkService.getYuyue();
        List<ResListVo> resListVoList = resourceService.getResList();
        ReservationVo reservationVo = new ReservationVo();
        if (u_id != null){
            reservationVo = resourceService.getReservationVo(u_id);
            List<SignIn> signIns = signInService.list(new QueryWrapper<SignIn>().eq("u_id",u_id));
            for (int i = 0; i < signIns.size(); i++) {
                int r_id = signIns.get(i).getR_id();
                for (int j = 0; j < resListVoList.size(); j++) {
                    if (resListVoList.get(j).getR_id() == r_id){
                        resListVoList.get(j).setIs_have_sign_in(1);
                    } else {
                        resListVoList.get(j).setIs_have_sign_in(0);
                    }
                }
            }
        }

        List<ResourceCategory> categoryList = resourceCategoryService.list();
        List<ResCateVo> resCateVos = new ArrayList<>();
        for (int i = 0; i < categoryList.size(); i++) {
            ResCateVo resCateVo = new ResCateVo(categoryList.get(i).getRc_id(),categoryList.get(i).getRc_title());
            resCateVos.add(resCateVo);
        }

        result.put("res_cate_list",resCateVos);
        result.put("res_list",resListVoList);
        result.put("yy_info",yuYueVo);
        result.put("reservation",reservationVo);


        return result;
    }
}
